<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Logistic-Regression | Bulua</title><meta name="keywords" content="逻辑回归, Logistic Regression"><meta name="author" content="Bulua"><meta name="copyright" content="Bulua"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="机器学习算法中的监督式学习可以分为2大类：    分类模型：目标变量是分类变量（离散值）；   回归模型：目标变量是连续性数值变量。   逻辑回归通常用于解决分类问题，例如，业界经常用它来预测：客户是否会购买某个商品，借款人是否会违约等等。">
<meta property="og:type" content="article">
<meta property="og:title" content="Logistic-Regression">
<meta property="og:url" content="http://bulua.gitee.io/2021/10/11/Logistic-Regression/index.html">
<meta property="og:site_name" content="Bulua">
<meta property="og:description" content="机器学习算法中的监督式学习可以分为2大类：    分类模型：目标变量是分类变量（离散值）；   回归模型：目标变量是连续性数值变量。   逻辑回归通常用于解决分类问题，例如，业界经常用它来预测：客户是否会购买某个商品，借款人是否会违约等等。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/bulua/bulua_img/raw/master/download.jpg">
<meta property="article:published_time" content="2021-10-11T11:29:24.000Z">
<meta property="article:modified_time" content="2021-10-11T11:46:36.460Z">
<meta property="article:author" content="Bulua">
<meta property="article:tag" content="机器学习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/bulua/bulua_img/raw/master/download.jpg"><link rel="shortcut icon" href="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg"><link rel="canonical" href="http://bulua.gitee.io/2021/10/11/Logistic-Regression/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"Traditional Chinese Activated Manually","cht_to_chs":"Simplified Chinese Activated Manually","day_to_night":"Dark Mode Activated Manually","night_to_day":"Light Mode Activated Manually","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Logistic-Regression',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-11 19:46:36'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">3</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/bulua/bulua_img/raw/master/download.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Bulua</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Logistic-Regression</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2021-10-11T11:29:24.000Z" title="Created 2021-10-11 19:29:24">2021-10-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2021-10-11T11:46:36.460Z" title="Updated 2021-10-11 19:46:36">2021-10-11</time></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Logistic-Regression"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p> 机器学习算法中的监督式学习可以分为2大类： </p>
<ul>
<li> <strong>分类模型</strong>：目标变量是分类变量（离散值）； </li>
<li> <strong>回归模型</strong>：目标变量是连续性数值变量。 </li>
</ul>
<p>逻辑回归通常用于解决分类问题，例如，业界经常用它来预测：客户是否会购买某个商品，借款人是否会违约等等。 </p>
<span id="more"></span>

<p>实际上，<strong>“分类”是应用逻辑回归的目的和结果，但中间过程依旧是“回归”</strong>。</p>
<p>为什么这么说？</p>
<p>因为通过逻辑回归模型，我们得到的计算结果是0-1之间的连续数字，可以把它称为“<strong>可能性</strong>”（概率）。对于上述问题，就是：客户购买某个商品的可能性，借款人违约的可能性。</p>
<p>然后，给这个可能性加一个阈值，就成了分类。例如，算出贷款违约的可能性&gt;0.5，将借款人预判为坏客户。</p>
<h2 id="1、线性回归"><a href="#1、线性回归" class="headerlink" title="1、线性回归"></a>1、线性回归</h2><p>考虑最简单的情况，即只有一个自变量的情况。比方说广告投入金额x和销售量y的关系，散点图如下，这种情况适用一元线性回归。 </p>
<p><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/v2-e277ce00fed265369f403381ccd34766_r.jpg"></p>
<p>但在许多实际问题中，因变量y是分类型，只取0、1两个值，和x的关系不是上面那样。假设我们有这样一组数据：给不同的用户投放不同金额的广告，记录他们购买广告商品的行为，1代表购买，0代表未购买。 </p>
<p><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/v2-17549f0c5282036306c84107ffa8cff9_720w.jpg"></p>
<p>假如此时依旧考虑线性回归模型，得到如下拟合曲线： </p>
<p><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/v2-455ebb504864589ac07e24d75e60e7fa_720w.jpg"></p>
<p>线性回归拟合的曲线，看起来和散点毫无关系，似乎没有意义。但我们可以在计算出 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Chat%7By%7D" alt="[公式]"> 的结果后，加一个限制，即 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Chat%7By%7D%3E0.5" alt="[公式]"> ，就认为其属于1这一类，购买了商品，否则认为其不会购买，即： </p>
<p><img src= "" data-lazy-src="https://pic2.zhimg.com/80/v2-f5fd31acc8a035ba70c96effe9d49ef1_720w.jpg"></p>
<p> 由于拟合方程为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Chat%7By%7D=0.34*x" alt="[公式]"> ，那么上面的限制就等价于： </p>
<p><img src= "" data-lazy-src="https://pic1.zhimg.com/80/v2-0449350970b9a4fdea01ec883f6d8838_720w.jpg"></p>
<p> 这种形式，非常像单位阶跃函数： </p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-d82759ab539b50f7aa34641c47caf1be_720w.jpg"></p>
<p> 图像如下： </p>
<p><img src= "" data-lazy-src="https://pic4.zhimg.com/80/v2-6e12db828d4dabc51af0e3a86f25b7e7_720w.jpg"></p>
<p>我们发现，把阶跃函数向右平移一下，就可以比较好地拟合上面的散点图呀！但是阶跃函数有个问题，它不是连续函数。</p>
<p>理想的情况，是像线性回归的函数一样，<strong>X和Y之间的关系，是用一个单调可导的函数来描述的</strong>。</p>
<h2 id="2、sigmond函数"><a href="#2、sigmond函数" class="headerlink" title="2、sigmond函数"></a>2、sigmond函数</h2><p>实际上，逻辑回归算法的拟合函数，叫做sigmond函数：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(z)=%5Cfrac%7B1%7D%7B1+e%5E%7B-z%7D%7D" alt="[公式]"></p>
<p>函数图像如下（百度图片搜到的图）：</p>
<p><img src= "" data-lazy-src="https://pic2.zhimg.com/80/v2-c3c88e714d7327e527bb5a2e62639521_720w.jpg" alt="img"></p>
<p>sigmoid函数是一个s形曲线，就像是阶跃函数的温和版，阶跃函数在0和1之间是突然的起跳，而sigmoid有个平滑的过渡。</p>
<p>从图形上看，sigmoid曲线就像是被掰弯捋平后的线性回归直线，<strong>将取值范围(−∞,+∞)映射到(0,1) 之间，更适宜表示预测的概率，即事件发生的“可能性”</strong> 。</p>
<h2 id="3、推广至多元场景"><a href="#3、推广至多元场景" class="headerlink" title="3、推广至多元场景"></a>3、推广至多元场景</h2><p>在<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/137713040">用人话讲明白梯度下降Gradient Descent</a>一文中，我们讲了多元线性回归方程的一般形式为：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=y=%7B%5Cbeta%7D_%7B0%7D+%7B%5Cbeta%7D_%7B1%7D+%7Bx%7D_%7B1%7D+%7B%5Cbeta%7D_%7B2%7D+%7Bx%7D_%7B2%7D+%5Ccdots+%7B%5Cbeta%7D_%7Bp%7D%7Bx%7D_%7Bp%7D" alt="[公式]"></p>
<p>可以简写为矩阵形式： <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cboldsymbol%7BY%7D=%5Cboldsymbol%7BX%7D%5Cboldsymbol%7B%5Cbeta%7D" alt="[公式]"></p>
<p>其中，</p>
<p><img src= "" data-lazy-src="https://pic3.zhimg.com/80/v2-728f8a07406692b51ff89b6b2aa66306_720w.jpg" alt="img"></p>
<p>将特征加权求和Xβ（后面不对矩阵向量加粗了，大家应该都能理解）代入sigmond函数中的z，得到 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B1%7D%7B1+e%5E%7B-X%5Cbeta%7D%7D" alt="[公式]"> ，令其为预测为正例的概率P(Y=1)，那么逻辑回归的形式就有了：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(Y=1)=%5Cfrac%7B1%7D%7B1+e%5E%7B-X%5Cbeta%7D%7D" alt="[公式]"></p>
<p>到目前为止，逻辑函数的构造算是完成了。找到了合适的函数，下面就是求函数中的未知参数向量β了。求解之前，我们需要先理解一个概念——<strong>似然性</strong>。</p>
<h2 id="4、似然函数"><a href="#4、似然函数" class="headerlink" title="4、似然函数"></a>4、似然函数</h2><p>我们常常用<strong>概率(Probability)</strong> 来描述一个事件发生的可能性。</p>
<p>而<strong>似然性(Likelihood)</strong> 正好反过来，意思是一个事件实际已经发生了，反推在什么参数条件下，这个事件发生的概率最大。</p>
<p>用数学公式来表达上述意思，就是:</p>
<ul>
<li>已知参数 β 前提下，预测某事件 x 发生的条件概率为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(x+%7C+%5Cbeta)" alt="[公式]"> ;</li>
<li>已知某个已发生的事件 x，未知参数 β 的似然函数为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta%7C+x)" alt="[公式]"> ；</li>
<li>上面两个值相等，即: <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta%7C+x)=P(x+%7C+%5Cbeta)" alt="[公式]"> 。</li>
</ul>
<p>一个参数 β 对应一个似然函数的值，当 β 发生变化， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta%7C+x)" alt="[公式]"> 也会随之变化。当我们在取得某个参数的时候，似然函数的值到达了最大值，说明在这个参数下最有可能发生x事件，即这个参数最合理。</p>
<p><strong>因此，最优β，就是使当前观察到的数据出现的可能性最大的β。</strong></p>
<h2 id="5、最大似然估计"><a href="#5、最大似然估计" class="headerlink" title="5、最大似然估计"></a>5、最大似然估计</h2><p>在二分类问题中，y只取0或1，可以组合起来表示y的概率:</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(y)=P(y=1)%5E%7By%7DP(y=0)%5E%7B1-y%7D" alt="[公式]"></p>
<p>我们可以把y=1代入上式验证下：</p>
<ul>
<li>左边是P(y=1)；</li>
<li>右边是 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(y=1)%5E1P(y=0)%5E0" alt="[公式]"> ，也为P(y=1)。</li>
</ul>
<p>上面的式子，更严谨的写法需要加上特征x和参数β：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(y%7Cx,%5Cbeta)=P(y=1%7Cx,%5Cbeta)%5E%7By%7D%5B1-P(y=1%7Cx,%5Cbeta)%5D%5E%7B1-y%7D" alt="[公式]"></p>
<p>前面说了， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B1%7D%7B1+e%5E%7B-x%7B%5Cbeta%7D%7D%7D" alt="[公式]"> 表示的就是P(y=1)，代入上式：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(y%7Cx,%5Cbeta)=%5Cleft(%5Cfrac%7B1%7D%7B1+e%5E%7B-+x%7B%5Cbeta%7D+%7D%7D%5Cright)%5E%7By%7D%5Cleft(1-%5Cfrac%7B1%7D%7B1+e%5E%7B-x%7B%5Cbeta%7D%7D%7D%5Cright)%5E%7B1-y%7D" alt="[公式]"></p>
<p>根据上一小节说的最优β的定义，也就是最大化我们见到的样本数据的概率，即求下式的最大值。</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta)=%5Cprod_%7Bi=1%7D%5E%7Bn%7DP(y_%7Bi%7D%7Cx_%7Bi%7D,%5Cbeta)=%5Cprod_%7Bi=1%7D%5E%7Bn%7D%5Cleft(%5Cfrac%7B1%7D%7B1+e%5E%7B+-%7Bx%7D%7Bi%7D%7B%5Cbeta%7D+%7D%7D%5Cright)%5E%7By%7Bi%7D%7D%5Cleft(1-%5Cfrac%7B1%7D%7B1+e%5E%7B-%7Bx%7D_%7Bi%7D%7B%5Cbeta%7D%7D%7D%5Cright)%5E%7B1-y_i%7D" alt="[公式]"></p>
<p>这个式子怎么来的呢？</p>
<p>其实很简单。</p>
<p>前面我们说了， <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta%7C+x)=P(x+%7C+%5Cbeta)" alt="[公式]"> ，对于某个观测值yi，似然函数的值 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cmathcal%7BL%7D(%5Cbeta%7C+y_%7Bi%7D)" alt="[公式]"> ，就等于条件概率的值 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=P(y_%7Bi%7D+%7C+%5Cbeta)" alt="[公式]"> 。</p>
<p>另外我们知道，如果事件A与事件B相互独立，那么两者同时发生的概率为P(A)*P(B)。那么我们观测到的y1,y2……yn，他们同时发生的概率就是 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cprod_%7Bi=1%7D%5E%7Bn%7DP(y_%7Bi%7D%7C%5Cbeta)" alt="[公式]"> 。</p>
<p>因为一系列的xi和yi都是我们实际观测到的数据，式子中未知的只有β。因此，现在问题就变成了<strong>求β在取什么值的时候，L(β)能达到最大值。</strong></p>
<p>L(β)是所有观测到的y发生概率的乘积，这种情况求最大值比较麻烦，一般我们会先取对数，将乘积转化成加法。</p>
<p>取对数后，转化成下式：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=log%5Cmathcal%7BL%7D(%5Cbeta)=+%5Csum_%7Bi=1%7D%5E%7Bn%7D%5Cleft(%5By_%7Bi%7D%5Ccdot+log(%5Cfrac%7B1%7D%7B1+e%5E%7B+-%7Bx%7D_%7Bi%7D%7B%5Cbeta%7D%7D%7D)%5D+%5B(1-y_%7Bi%7D)+%5Ccdot+log(1-%5Cfrac%7B1%7D%7B1+e%5E%7B-%7Bx%7D_%7Bi%7D%7B%5Cbeta%7D%7D%7D)%5D%5Cright)" alt="[公式]"></p>
<p>接下来想办法求上式的最大值就可以了，求解前，我们要提一下逻辑回归的损失函数。</p>
<h2 id="6、损失函数"><a href="#6、损失函数" class="headerlink" title="6、损失函数"></a>6、损失函数</h2><p>在机器学习领域，总是避免不了谈论损失函数这一概念。<strong>损失函数是用于衡量预测值与实际值的偏离程度，即模型预测的错误程度</strong>。也就是说，这个值越小，认为模型效果越好，举个极端例子，如果预测完全精确，则损失函数值为0。</p>
<p>在线性回归一文中，我们用到的损失函数是残差平方和SSE：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=Q=%5Csum_%7B1%7D%5E%7Bn%7D%5Cleft(y_%7Bi%7D-%5Chat%7By%7D_%7Bi%7D%5Cright)%5E%7B2%7D=%5Csum_%7B1%7D%5E%7Bn%7D%5Cleft(y_%7Bi%7D-%7Bx%7D_%7Bi%7D%7B%5Cbeta%7D%5Cright)%5E%7B2%7D" alt="[公式]"></p>
<p>这是个凸函数，有全局最优解。</p>
<p>如果逻辑回归也用平方损失，那么就是：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=Q=%5Csum_%7B1%7D%5E%7Bn%7D%5Cleft(y_%7Bi%7D-%5Cfrac%7B1%7D%7B1+e%5E%7B+-%7Bx%7D_%7Bi%7D%7B%5Cbeta%7D%7D%7D%5Cright)%5E%7B2%7D" alt="[公式]"></p>
<p>很遗憾，这个不是凸函数，不易优化，容易陷入局部最小值，所以逻辑函数用的是别的形式的函数作为损失函数，叫<strong>对数损失函数</strong>（log loss function）。</p>
<p>这个对数损失，就是上一小节的<strong>似然函数取对数后，再取相反数</strong>哟：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=J(%5Cbeta)+=-log%5Cmathcal%7BL%7D(%5Cbeta)=-+%5Csum_%7Bi=1%7D%5E%7Bn%7D%5Cleft%5By_ilogP(y_i)+(1-y_i)log(1-P(y_i))%5Cright%5D" alt="[公式]"></p>
<p>这个对数损失函数好理解吗？我还是举个具体例子吧。</p>
<p>用文章开头那个例子，假设我们有一组样本，建立了一个逻辑回归模型P(y=1)=f(x)，其中一个样本A是这样的：</p>
<p>公司花了x=1000元做广告定向投放，某个用户看到广告后购买了，此时实际的y=1，f(x=1000)算出来是0.6，这里有-0.4的偏差，是吗？在逻辑回归中不是用差值计算偏差哦，用的是对数损失，所以它的偏差定义为log0.6（其实也很好理解为什么取对数，因为我们算的是P(y=1)，如果算出来的预测值正好等于1，那么log1=0，偏差为0）。</p>
<p>样本B：x=500，y=0，f(x=500)=0.3，偏差为log(1-0.3)=log0.7。</p>
<p>根据log函数的特性，自变量取值在[0,1]间，log出来是负值，而损失一般用正值表示，所以要取个相反数。因此计算A和B的总损失，就是：-log0.6-log0.7。</p>
<p>下面我们用梯度下降法求损失函数的最小值（也可以用梯度上升算法求似然函数的最大值，这两是等价的）。</p>
<h2 id="7、梯度下降法求解"><a href="#7、梯度下降法求解" class="headerlink" title="7、梯度下降法求解"></a>7、梯度下降法求解</h2><p>要开始头疼的公式推导部分了，不要害怕哦，我们还是从最简单的地方开始，非常容易看懂。</p>
<p>首先看，对于sigmoid函数 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(x)=%5Cfrac%7B1%7D%7B1+e%5E%7B-x%7D%7D" alt="[公式]"> ，f’(x)等于多少？</p>
<p>如果你还记得导数表中这2个公式，那就好办了（不记得也没关系，这就给你列出来）：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=(%5Cfrac%7B1%7D%7Bx%7D)%5E%7B%5Cprime%7D=-%5Cfrac%7B1%7D%7Bx%5E%7B2%7D%7D" alt="[公式]"></p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=(e%5E%7Bx%7D)%5E%7B%5Cprime%7D=e%5E%7Bx%7D" alt="[公式]"></p>
<p>根据上两个公式，推导：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f%27(x)=(%5Cfrac%7B1%7D%7B1+e%5E%7B-x%7D%7D)%27=-%5Cfrac%7B(e%5E%7B-x%7D)%27%7D%7B(1+e%5E%7B-x%7D)%5E%7B2%7D%7D=%5Cfrac%7Be%5E%7B-x%7D%7D%7B(1+e%5E%7B-x%7D)%5E%7B2%7D%7D" alt="[公式]"></p>
<p>到这还不算完哦，我们发现 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=1-f(x)=%5Cfrac%7Be%5E%7B-x%7D%7D%7B1+e%5E%7B-x%7D%7D" alt="[公式]"> ，而f’(x)正好可以拆分为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7Be%5E%7B-x%7D%7D%7B1+e%5E%7B-x%7D%7D%5Ccdot%5Cfrac%7B1%7D%7B1+e%5E%7B-x%7D%7D" alt="[公式]"> ，也就是说：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f%27(x)=f(x)%5Ccdot(1-f(x))" alt="[公式]"></p>
<p>当然，现在我们的x是已知的，未知的是β，所以后面是对β求导，记：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B1%7D%7B1+e%5E%7B-x_i%5Cbeta%7D%7D=f(x_i%5Cbeta)+" alt="[公式]"></p>
<p>把它代入前面我们得到逻辑回归的损失函数：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=J(%5Cbeta)+=-+%5Csum_%7Bi=1%7D%5E%7Bn%7D%5Cleft%5By_ilog(f(x_i%5Cbeta))+(1-y_i)log(1-f(x_i%5Cbeta))%5Cright%5D=-+%5Csum_%7Bi=1%7D%5E%7Bn%7Dg(%5Cbeta_i,x_i,y_i)" alt="[公式]"></p>
<p>简便起见，先撇开求和号看g(β,x,y)。不过这个g(β,x,y)里面也挺复杂的，我们再把里面的 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=f(x_i%5Cbeta)" alt="[公式]"> 挑出来，单独先看它对β向量中的某个βj求偏导是什么样。</p>
<p>根据上面的求导公式，有：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B%5Cpartial+f(x_i%5Cbeta)+%7D%7B%5Cpartial+%5Cbeta_%7Bj%7D%7D+=+f(x_i%5Cbeta)%5Ccdot(1-f(x_i%5Cbeta))%5Ccdot+x_%7Bij%7D" alt="[公式]"></p>
<p>注意咯，这个xi实际上指的是第i个样本的特征向量，即 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=(1,+x_%7Bi1%7D,+...,x_%7Bip%7D)" alt="[公式]"> ，其中只有xij会和βj相乘，因此求导后整个xi只剩xij了。</p>
<p>理解了前面说的，下面的化简就轻而易举（知乎不能正常显示这个公式，只好写完转成图片粘过来）：</p>
<p><img src= "" data-lazy-src="https://pic2.zhimg.com/80/v2-ce67a6f59a69426719d2b23a007252b5_720w.jpg" alt="img"></p>
<p>加上求和号：</p>
<p><img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5Cfrac%7B%5Cpartial+J(%5Cbeta)%7D%7B%5Cpartial+%5Cbeta_%7Bj%7D%7D+=+-+%5Csum_%7Bi=1%7D%5E%7Bn%7D+%5Cleft(y_i-f(x_i%5Cbeta)%5Cright)+%5Ccdot+x_%7Bij%7D+=++%5Csum_%7Bi=1%7D%5E%7Bn%7D+%5Cleft(%5Cfrac%7B1%7D%7B1+e%5E%7B-x_i%5Cbeta%7D%7D-y_i%5Cright)+%5Ccdot+x_%7Bij%7D" alt="[公式]"></p>
<p>有了偏导，也就有了梯度G，即偏导函数组成的向量。</p>
<p><strong>梯度下降算法过程：</strong></p>
<ol>
<li><p>初始化β向量的值，即 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5CTheta_%7B0%7D" alt="[公式]"> ，将其代入G得到当前位置的梯度；</p>
</li>
<li><p>用步长α乘以当前梯度，得到从当前位置下降的距离；</p>
</li>
<li><p>更新 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5CTheta_1" alt="[公式]"> ，其更新表达式为 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5CTheta_1=%5CTheta_0-%5Calpha+G" alt="[公式]"> ；</p>
</li>
<li><p>重复以上步骤，直到更新到某个 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5CTheta_k" alt="[公式]"> ，达到停止条件，这个 <img src= "" data-lazy-src="https://www.zhihu.com/equation?tex=%5CTheta_k" alt="[公式]"> 就是我们求解的参数向量。</p>
</li>
</ol>
<p>转载链接：<a target="_blank" rel="noopener" href="https://zhuanlan.zhihu.com/p/139122386">https://zhuanlan.zhihu.com/p/139122386</a></p>
<p>原创博主：化简可得</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">Bulua</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://bulua.gitee.io/2021/10/11/Logistic-Regression/">http://bulua.gitee.io/2021/10/11/Logistic-Regression/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0/">机器学习</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/10/12/svm-shang/"><img class="prev-cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">支持向量机（上）</div></div></a></div><div class="next-post pull-right"><a href="/2021/09/16/zui-hou-yi-kuai-shi-tou-de-zhong-liang/"><img class="next-cover" src= "" data-lazy-src="https://cdn.jsdelivr.net/gh/18WantTobeSinger/PictureBed/LeetCode-cover.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">最后一块石头的重量</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2021/10/19/Kernel-Function/" title="核函数"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-19</div><div class="title">核函数</div></div></a></div><div><a href="/2021/11/15/PCA/" title="PCA"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-15</div><div class="title">PCA</div></div></a></div><div><a href="/2021/11/15/LDA/" title="LDA"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-11-15</div><div class="title">LDA</div></div></a></div><div><a href="/2021/10/12/svm-shang/" title="支持向量机（上）"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-12</div><div class="title">支持向量机（上）</div></div></a></div><div><a href="/2021/10/12/svm-xia/" title="支持向量机（下）"><img class="cover" src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-10-12</div><div class="title">支持向量机（下）</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/tubiao.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Bulua</div><div class="author-info__description">No matter how far you may fly, never forget where you come from.</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">3</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/Bulua" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%E3%80%81%E7%BA%BF%E6%80%A7%E5%9B%9E%E5%BD%92"><span class="toc-number">1.</span> <span class="toc-text">1、线性回归</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%E3%80%81sigmond%E5%87%BD%E6%95%B0"><span class="toc-number">2.</span> <span class="toc-text">2、sigmond函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%E3%80%81%E6%8E%A8%E5%B9%BF%E8%87%B3%E5%A4%9A%E5%85%83%E5%9C%BA%E6%99%AF"><span class="toc-number">3.</span> <span class="toc-text">3、推广至多元场景</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4%E3%80%81%E4%BC%BC%E7%84%B6%E5%87%BD%E6%95%B0"><span class="toc-number">4.</span> <span class="toc-text">4、似然函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5%E3%80%81%E6%9C%80%E5%A4%A7%E4%BC%BC%E7%84%B6%E4%BC%B0%E8%AE%A1"><span class="toc-number">5.</span> <span class="toc-text">5、最大似然估计</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6%E3%80%81%E6%8D%9F%E5%A4%B1%E5%87%BD%E6%95%B0"><span class="toc-number">6.</span> <span class="toc-text">6、损失函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7%E3%80%81%E6%A2%AF%E5%BA%A6%E4%B8%8B%E9%99%8D%E6%B3%95%E6%B1%82%E8%A7%A3"><span class="toc-number">7.</span> <span class="toc-text">7、梯度下降法求解</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/11/15/LDA/" title="LDA"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LDA"/></a><div class="content"><a class="title" href="/2021/11/15/LDA/" title="LDA">LDA</a><time datetime="2021-11-15T12:54:12.000Z" title="Created 2021-11-15 20:54:12">2021-11-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/11/15/PCA/" title="PCA"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="PCA"/></a><div class="content"><a class="title" href="/2021/11/15/PCA/" title="PCA">PCA</a><time datetime="2021-11-15T12:34:48.000Z" title="Created 2021-11-15 20:34:48">2021-11-15</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/19/Kernel-Function/" title="核函数"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="核函数"/></a><div class="content"><a class="title" href="/2021/10/19/Kernel-Function/" title="核函数">核函数</a><time datetime="2021-10-19T11:13:40.000Z" title="Created 2021-10-19 19:13:40">2021-10-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/12/svm-xia/" title="支持向量机（下）"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="支持向量机（下）"/></a><div class="content"><a class="title" href="/2021/10/12/svm-xia/" title="支持向量机（下）">支持向量机（下）</a><time datetime="2021-10-12T00:31:00.000Z" title="Created 2021-10-12 08:31:00">2021-10-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/10/12/svm-shang/" title="支持向量机（上）"><img src= "" data-lazy-src="https://gitee.com/bulua/bulua_img/raw/master/download.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="支持向量机（上）"/></a><div class="content"><a class="title" href="/2021/10/12/svm-shang/" title="支持向量机（上）">支持向量机（上）</a><time datetime="2021-10-12T00:30:53.000Z" title="Created 2021-10-12 08:30:53">2021-10-12</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021  <i id="heartbeat" class="fa fas fa-heartbeat"></i> Bulua</div></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/HCLonely/images@master/others/heartbeat.min.css"></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="Switch Between Traditional Chinese And Simplified Chinese">繁</button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'K7ke6VBNMaIjRIWWht9uCbsT-gzGzoHsz',
      appKey: 'rl09GFl8gOiYw6OxIIIuOzur',
      placeholder: 'Please leave your footprints',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'en',
      recordIP: false,
      serverURLs: '',
      emojiCDN: '',
      emojiMaps: "",
      enableQQ: true,
      path: window.location.pathname,
      requiredFields: ["nick,mail"],
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script></div><div class="aplayer no-destroy" data-id="186016" data-server="netease" data-type="song" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay=true muted></div><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="true" data-click="true"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>